package stream;

import java.util.*;
import java.util.function.Function;
import java.util.function.IntFunction;
import java.util.stream.Collectors;

public class Demo3 {
    public static void main(String[] args) {
        //将数据收集到单双列集合中
        //单列
        //1.创建集合并添加数据
        ArrayList<String> list = new ArrayList<>();
        Collections.addAll(list, "jack-man-22", "tom-man-22", "mary-wom-23");
        //2.创建流对象创建过滤器获取man
        String[] arr = list.stream().filter(s -> "man".equals(s.split("-")[1])).toArray(new IntFunction<String[]>() {
            @Override
            public String[] apply(int value) {
                return new String[value];
            }
        });
        System.out.println(Arrays.toString(arr));
        //3.将man添加到双列集合中
      /*  Map<String, Integer> map = list.stream().filter(s -> "man".equals(s.split("-")[1])).collect(
                Collectors.toMap(
                        new Function<String, String>() {
                            @Override
                            public String apply(String s) {
                                return s.split("-")[0];
                            }
                        },
                        new Function<String, Integer>() {
                            @Override
                            public Integer apply(String s) {
                                return Integer.parseInt(s.split("-")[2]);
                            }

                        }));

        System.out.println(map);*/
        //使用lambda表达式
        Map<String, String> map = list.stream()
                .filter(s -> "man".equals(s.split("-")[1]))
                .collect(Collectors.toMap(
                        s -> s.split("-")[0],
                        s -> s.split("-")[2])
                );
        System.out.println(map);
        System.out.println("-----------------------------");
        //添加到set中
        Set<String> sets = list.stream().filter(s -> "wom".equals(s.split("-")[1])).collect(Collectors.toSet());
        System.out.println(sets);

    }
}
